package com.hp.bon.sgw.service;

import com.google.gson.Gson;
import com.hp.bon.sgw.bean.CapabilityCtrlBean;
import com.hp.bon.sgw.bean.CongestionBean;
import com.hp.bon.sgw.bean.SGWStatCapaBean;
import com.hp.bon.sgw.bean.SGWStatMessageBean;
import com.hp.bon.sgw.bean.SGWStatSummaryBean;
import com.hp.bon.sgw.bean.SysPertriNetBean;
import com.hp.bon.sgw.domain.Node;
import com.hp.bon.sgw.util.dcc.DccAvpHelper;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/")
public class SGWCoreSysCtrlService extends SysCtrlService
{
  private final long startTime = System.currentTimeMillis();

  @GET
  @Path("sgwstat/getStatToday/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatToday(@PathParam("module") String module, @PathParam("node") String node) { return getSystemStatusSum(module, SGWStatSummaryBean.Time_Flag_Today); } 
  @GET
  @Path("sgwstat/getStatFromStart/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatFromStart(@PathParam("module") String module, @PathParam("node") String node) {
    return getSystemStatusSum(module, SGWStatSummaryBean.Time_Flag_FromStart);
  }
  @GET
  @Path("sgwstat/getStatHourModule/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatHourModule(@PathParam("module") String module, @PathParam("node") String node) { return getSystemStatusSum(module, SGWStatSummaryBean.Time_Flag_Hour); } 
  @GET
  @Path("sgwstat/getStatThruput/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatThruput(@PathParam("module") String module, @PathParam("node") String node) {
    return getSystemStatusThrupt(module);
  }
  @GET
  @Path("sgwstat/getStatQueue/{module:.*}/{node}")
  @Produces({"application/json"})
  public String getStatQueue(@PathParam("module") String module, @PathParam("node") String node) { return getStatQueue(module); }


  public String getSystemStatusSum(String module, int timeFlag)
  {
    SGWStatSummaryBean sumBean = new SGWStatSummaryBean();
    Calendar startTime = Calendar.getInstance();
    startTime.setTimeInMillis(System.currentTimeMillis());
    if (SGWStatSummaryBean.Time_Flag_FromStart == timeFlag) {
      startTime.setTimeInMillis(this.startTime);
    } else if (SGWStatSummaryBean.Time_Flag_Today == timeFlag) {
      startTime.set(11, 0);
      startTime.set(12, 0);
      startTime.set(13, 0);
      startTime.set(14, 0);
    } else {
      startTime.set(12, 0);
      startTime.set(13, 0);
      startTime.set(14, 0);
    }
    SGWStatMessageBean statBean = null;
    if ((module == null) || ("".equals(module.trim()))) {
      statBean = this.dataStatusServ.statMessage(null, new Timestamp(startTime.getTimeInMillis()));
    } else {
      String nodeHostID = module.trim() + "@" + this.confSrv.getRealm();
      Node node = this.confSrv.getNodeByName(nodeHostID);
      if (node != null)
        statBean = this.dataStatusServ.statMessage(Integer.valueOf(node.getId()), new Timestamp(startTime.getTimeInMillis()));
      else {
        statBean = new SGWStatMessageBean();
      }
    }
    sumBean.setMsgStat(statBean);

    sumBean.setCongessionInfo(this.congestionBean.toStatBean());
    sumBean.setPortInfo(getServerPorts());
    String serverNameAddr = this.confSrv.getValue("system.sgw_server_name", "HP SGW Server") + " at " + this.confSrv.getValue("system.host_server_name", "SGW Server ") + "[" + this.confSrv.getValue("system.ip", "localhost") + "]" + " powered by HP " + 
      this.pertriBean.getPertiStatusName();
    sumBean.setOnlineServerName(serverNameAddr);

    sumBean.setNeUsedCapaInfo(fillPriortyInfo(this.dataStatusServ.getAccessedCapabilities(new Timestamp(startTime.getTimeInMillis()))));
    sumBean.setVersion(this.confSrv.getValue("system.version", null));
    return new Gson().toJson(sumBean);
  }

  private List<SGWStatCapaBean> fillPriortyInfo(List<SGWStatCapaBean> actvCapBeans)
  {
    Map capMap = this.confSrv.getCapabilityCtrlMap();

    for (SGWStatCapaBean capBean : actvCapBeans) {
      String capability = capBean.getCapaInfo();
      if (capability.contains("@")) {
        capability = DccAvpHelper.getCapability(capability);
        capBean.setCapaInfo(capability);
      }
      CapabilityCtrlBean capCtrlBean = (CapabilityCtrlBean)capMap.get(capability);
      capBean.setPriority(capCtrlBean != null ? capCtrlBean.getPriority() +"": "default");
    }
    return actvCapBeans;
  }
}